// pages/index/home.js
var app = getApp();
var http_service = app.data.service;
Page({
  data: {
    text: "This is page data.",
    service: http_service,
    url: '/public/uploads/20180223/84a50d54dffc45926665ebc012c28eb7.png',
    time: '',
    area: [],
    index:0,
    store:[],
    s_index:0,
    lat:0,
    lng:0,
    distance:[],
    //服务id
    serviceId:0,
  },
  onLoad: function (options) {
    var that = this;
    var serviceId = options.serviceId;
    //console.log(serviceId)
    that.setData({
      serviceId:serviceId
    })
    /*
      wx.authorize({
        scope: 'scope.userInfo',
        success:function(res){
          console.log(res.errMsg)
        }
      })
    */
    
    var that = this;
    //距离数据
    var distances = new Array();
    wx.request({
      url: http_service+'/index.php/getareabyserviceid', 
      data: {
        serviceId: serviceId
      },
      header: {
        'content-type': 'application/json' // 默认值
      },
      success: function (res) {
        that.setData({
          area : res.data,
        },function(){
          if(res.data.length>0){
            wx.request({
              url: http_service + '/index.php/getstore',
              data: {
                serviceId: serviceId
              },
              header: {
                'content-type': 'application/json' // 默认值
              },
              success: function (res) {
                that.setData({
                  store: res.data
                }, function () {
                  //store设置成功就应该开始计算坐标
                  //获取坐标
                  wx.getLocation({
                    type: 'wgs84',
                    success: function (res) {
                      that.setData({
                        lat: res.latitude,
                        lng: res.longitude
                      })
                      for (var i = 0; i < that.data.store.length; i++) {
                        var radLat1 = that.data.store[i].lat * Math.PI / 180.0
                        var radLat2 = res.latitude * Math.PI / 180.0
                        var a = radLat1 - radLat2;
                        var b = that.data.store[i].lng * Math.PI / 180.0 - res.longitude * Math.PI / 180.0;
                        var s = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(a / 2), 2) +
                          Math.cos(radLat1) * Math.cos(radLat2) * Math.pow(Math.sin(b / 2), 2)));
                        s = s * 6378.137;// EARTH_RADIUS;
                        s = Math.round(s * 10000) / 10000; //输出为公里
                        distances[i] = s.toFixed(1);
                        that.setData({
                          distance: distances
                        })
                      }
                    }
                  })
                  //坐标计算结束，开始请求店铺
                })
              }
            })
          }
          
        })
      }
    })
  },
  onReady: function () {
   
  },
  onShow: function () {
    
  },
  onHide: function () {
    
  },
  onUnload: function () {
    
  },
  onPullDownRefresh: function () {
    
  },
  onReachBottom: function () {
    
  },
  onShareAppMessage: function () {
   
  },
  onPageScroll: function () {
   
  },
  onTabItemTap(item) {
    console.log(item.index)
    console.log(item.pagePath)
    console.log(item.text)
  },
  // click(){
  //   wx.downloadFile({
  //     url: http_service +'/public/uploads/20180306/84816ab23fc2daebfbb9e7a4b705338e.jpg', 
  //     success: function (res) {
  //        console.log(res);
  //       if (res.statusCode === 200) {
  //         var tempFilePaths = res.tempFilePath
  //         console.log(tempFilePaths)
  //         wx.saveFile({
  //           tempFilePath: tempFilePaths,
  //           success: function (res) {
  //             var savedFilePath = res.savedFilePath
  //             console.log(savedFilePath)
  //           }
  //         })
  //       }
  //     }
  //   })
  // },
  /*
   * 下载图片的方法
   */
  saveImgToPhotosAlbumTap: function (e) {
    // console.log(e.currentTarget.dataset);
    var url = e.currentTarget.dataset.url
    wx.downloadFile({
      url:url,
      success: function (res) {
        // console.log(res)
        wx.saveImageToPhotosAlbum({
          filePath: res.tempFilePath,
          success: function (res) {
            wx.showToast({
              title: '下载成功',
              icon: 'success',
              duration: 2000
            })
          },
          fail: function (res) {
          }
        })
      },
      fail: function () {
      }
    })
  },  


  // Event handler.
  bindPickerChange: function (e) {
    var that = this;
    //距离数据
    var distances = new Array();
    this.setData({
      index: e.detail.value
    })
    var area_id = that.data.area[that.data.index].id;
    wx.request({
      url: http_service + '/index.php/getstore',
      data: {
        area_id: area_id
      },
      header: {
        'content-type': 'application/json' // 默认值
      },
      success: function (res) {
        that.setData({
          store: res.data
        },function(){
          //store设置成功就应该开始计算坐标
          //获取坐标
          wx.getLocation({
            type: 'wgs84',
            success: function (res) {
              that.setData({
                lat: res.latitude,
                lng: res.longitude
              })
              for (var i = 0; i < that.data.store.length; i++) {
                var radLat1 = that.data.store[i].lat * Math.PI / 180.0
                var radLat2 = res.latitude * Math.PI / 180.0
                var a = radLat1 - radLat2;
                var b = that.data.store[i].lng * Math.PI / 180.0 - res.longitude * Math.PI / 180.0;
                var s = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(a / 2), 2) +
                  Math.cos(radLat1) * Math.cos(radLat2) * Math.pow(Math.sin(b / 2), 2)));
                s = s * 6378.137;// EARTH_RADIUS;
                s = Math.round(s * 10000) / 10000; //输出为公里
                distances[i] = s.toFixed(1);
                that.setData({
                  distance: distances
                })
              }
            }
          })
                //坐标计算结束，开始请求店铺
        })
      }
    })
  },



  //点击跳转的时候需要存入本地数据
  click:function(e){
    var that = this;
    var serviceId = that.data.serviceId;
    var storeId = e.currentTarget.id;
     //console.log(storeId)
    // console.log("home")
    wx.request({
      url: http_service + '/index.php/getservicebyid',
      data:{
        serviceId : serviceId
      },
      success:function(res){
        var price = res.data.price;
        wx.setStorage({
          key: 'affirm',
          data: {
            storeId: storeId,
            serviceId: serviceId,
            price: price,
            tec_id: 0,
            date: '未选择',
            c_date: 0,
            c_time: 0
          },
          success:function(){
            wx.navigateTo({
              url: 'subscribe/affirm?serviceId=' + serviceId + '&storeId=' + storeId,
            })
          }
        })
      }
    })
  }
})
